/*
* $Id: HousingManagerDialog.cpp 153 2010-09-26 17:33:07Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Alexey Volokitin
* All rights reserved
*
*/
#include "Common/include.h"
#include "HousingManagerDialog.h"
#include "Database/Tables/HousingsTable.h"
#include "Database/Tables/TypesTable.h"
#include "HousingDialog.h"
#include "Components/Journal/FamilyManager.h"

CHousingManagerDialog::housing::housing(int nTypeHousing, int nTypeFrame, int nFamily, std::tstring strName)
: m_nTypeHousing(nTypeHousing), m_nTypeFrame(nTypeFrame), m_nFamily(nFamily), m_strName(strName){}
CHousingManagerDialog::housing::housing()
: m_nTypeHousing(0), m_nTypeFrame(0), m_nFamily(0){}

CHousingManagerDialog::CHousingManagerDialog(CWnd* pParent)
: CDialog(IDD_HOUSE_MANAGE, pParent){
	IDatabase* pDb = IDatabaseManager::GetDatabaseManager()->OpenDatabase(JOURNAL_DATABASE_NAME);
	CTypeHousingsTable table;
	IResult* result = table.GetValue(pDb);
	for(int i = 0; i < (int)result->GetSizeResult(); i++){
		CTypeHousingsTable::content* pContent = (CTypeHousingsTable::content*)(*result)[i];
		if(pContent->s_strName.empty() && pContent->s_nId < 7){
			m_aTypesHousing.insert(std::make_pair(pContent->s_nId,
				ILanguageManager::GetLanguageManager()->GetString(EIGHT_FRAME_HOUSING_ID + pContent->s_nId)));
		}else
			m_aTypesHousing.insert(std::make_pair(pContent->s_nId, pContent->s_strName));
	}
	result->Free();
	CTypeFrameTable tableF;
	result = tableF.GetValue(pDb);
	for(int i = 0; i < (int)result->GetSizeResult(); i++){
		CTypeFrameTable::content* pContent = (CTypeFrameTable::content*)(*result)[i];
		if(pContent->s_strName.empty() && pContent->s_nId < 7){
			m_aTypesFrame.insert(std::make_pair(pContent->s_nId,
				ILanguageManager::GetLanguageManager()->GetString(S300_ID + pContent->s_nId)));
		}else
			m_aTypesFrame.insert(std::make_pair(pContent->s_nId, pContent->s_strName));
	}
	result->Free();
	IDatabaseManager::GetDatabaseManager()->CloseDatabase(pDb);
}
CHousingManagerDialog::~CHousingManagerDialog(){}

BEGIN_MESSAGE_MAP(CHousingManagerDialog, CDialog)
	ON_CONTROL(LBN_SELCHANGE, IDC_HOUSING_LIST, OnHouseSelChange)
	ON_CONTROL(LBN_SELCHANGE, IDC_FAMILY_LIST, OnFamilySelChange)
	ON_COMMAND(IDC_ADDH, OnHouseAdd)
	ON_COMMAND(IDC_ADDF, OnHouseFamilyAdd)
	ON_COMMAND(IDC_DELETEF, OnHouseFamilyDelete)
	ON_COMMAND(IDC_CHANGEH, OnHouseChange)
	ON_COMMAND(IDC_DELETEH, OnHouseDelete)
END_MESSAGE_MAP()

BOOL CHousingManagerDialog::OnInitDialog(){
	BOOL bRes = __super::OnInitDialog();
	SetWindowText(ILanguageManager::GetLanguageManager()->GetString(HOUSE_DIALOG_CAPTION_ID).c_str());
	SetDlgItemText(IDC_HOUSE_GROUP, ILanguageManager::GetLanguageManager()->GetString(HD_BLOCK_HOUSE_ID).c_str());
	SetDlgItemText(IDC_FAMILY_GROUP, ILanguageManager::GetLanguageManager()->GetString(HD_BLOCK_FAMILY_ID).c_str());
	SetDlgItemText(IDOK, ILanguageManager::GetLanguageManager()->GetString(CLOSED_ID).c_str());
	SetDlgItemText(IDC_ADDH, ILanguageManager::GetLanguageManager()->GetString(NEW_ID).c_str());
	SetDlgItemText(IDC_DELETEF, ILanguageManager::GetLanguageManager()->GetString(DELETE_ID).c_str());
	SetDlgItemText(IDC_NAMEH_STATIC, ILanguageManager::GetLanguageManager()->GetString(HD_NAME_HOUSING_ID).c_str());
	SetDlgItemText(IDC_TYPEH_STATIC, ILanguageManager::GetLanguageManager()->GetString(HD_TYPE_HOUSING_ID).c_str());
	SetDlgItemText(IDC_TYPEF_STATIC, ILanguageManager::GetLanguageManager()->GetString(HD_TYPE_FRAME_ID).c_str());
	SetDlgItemText(IDC_FAMILY_STATIC, ILanguageManager::GetLanguageManager()->GetString(HD_CONNECT_FAMILY_ID).c_str());
	SetDlgItemText(IDC_ADDF, ILanguageManager::GetLanguageManager()->GetString(ADD_ID).c_str());
	SetDlgItemText(IDC_CHANGEH, ILanguageManager::GetLanguageManager()->GetString(CHANGE_ID).c_str());
	SetDlgItemText(IDC_DELETEH, ILanguageManager::GetLanguageManager()->GetString(DELETE_ID).c_str());

	IDatabase* pDb = IDatabaseManager::GetDatabaseManager()->OpenDatabase(JOURNAL_DATABASE_NAME);
	CHousingsTable houses;
	IResult* pResult = houses.GetValue(pDb);
	for(int i = 0; i < (int)pResult->GetSizeResult(); i++){
		CHousingsTable::content* pContent = (CHousingsTable::content*)(*pResult)[i];
		if(!pContent->s_bDelete){
			m_aHousings.insert(std::make_pair(pContent->s_nId, housing(pContent->s_nType, pContent->s_nFrames, pContent->s_nIdFamily, pContent->s_strName)));
			m_HousingList.SetItemData(m_HousingList.AddString(pContent->s_strName.c_str()), pContent->s_nId);
			if(!pContent->s_nIdFamily)
				m_FreeHousing.SetItemData(m_FreeHousing.AddString(pContent->s_strName.c_str()), pContent->s_nId);
		}
	}
	pResult->Free();
	CFamilyManager* pManager = IJournalComponent::GetFamilyManager();
	std::map<int, CFamilyManager::family> aFamilies = pManager->GetFamilies();
	for(std::map<int, CFamilyManager::family>::iterator it = aFamilies.begin(); it != aFamilies.end(); it++){
		m_FamiliesList.SetItemData(m_FamiliesList.AddString(it->second.m_strName.c_str()), it->first);
		m_aFamilies.insert(std::make_pair(it->first, it->second.m_strName));
	}

	m_HousingList.SetCurSel(0);
	OnHouseSelChange();
	m_FreeHousing.SetCurSel(0);
	m_FamiliesList.SetCurSel(0);
	OnFamilySelChange();
	return bRes;
}

void CHousingManagerDialog::DoDataExchange(CDataExchange* pDX){
	__super::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_NAMEH_EDITREAD, m_NameHouse);
	DDX_Control(pDX, IDC_FAMILY_READ, m_FamilyHouse);
	DDX_Control(pDX, IDC_TYPEH_READ, m_TypeHouse);
	DDX_Control(pDX, IDC_TYPEF_READ, m_TypeFrame);
	DDX_Control(pDX, IDC_CHANGEH, m_ChangeHousing);
	DDX_Control(pDX, IDC_DELETEF, m_DeleteFamily);
	DDX_Control(pDX, IDC_DELETEH, m_DeleteHousing);
	DDX_Control(pDX, IDC_HOUSING_LIST, m_HousingList);
	DDX_Control(pDX, IDC_FAMILY_LIST, m_FamiliesList);
	DDX_Control(pDX, IDC_FAMILY_HOUSE_LIST, m_FamilyHouseList);
	DDX_Control(pDX, IDC_HOUSE_COMBO, m_FreeHousing);
}

void CHousingManagerDialog::OnHouseSelChange(){
	if(!m_HousingList.GetCount() || m_HousingList.GetCurSel() == -1){
		m_NameHouse.EnableWindow(false);
		m_ChangeHousing.EnableWindow(false);
		m_DeleteHousing.EnableWindow(false);
	}else{
		m_NameHouse.EnableWindow(true);
		m_ChangeHousing.EnableWindow(true);
		m_DeleteHousing.EnableWindow(true);
		CString str;
		m_HousingList.GetText(m_HousingList.GetCurSel(), str);
		m_NameHouse.SetWindowText(str.GetString());
		int nId = (int)m_HousingList.GetItemData(m_HousingList.GetCurSel());
		m_TypeHouse.SetWindowText(m_aTypesHousing[m_aHousings[nId].m_nTypeHousing].c_str());
		m_TypeFrame.SetWindowText(m_aTypesFrame[m_aHousings[nId].m_nTypeFrame].c_str());
		std::tstring strNameF = m_aFamilies[m_aHousings[nId].m_nFamily];
		m_FamilyHouse.SetWindowText(strNameF.c_str());
		if(strNameF.empty())
			m_FamilyHouse.EnableWindow(false);
		else
			m_FamilyHouse.EnableWindow(true);
	}
}

void CHousingManagerDialog::OnFamilySelChange(){
	if(!m_FamiliesList.GetCount() || m_FamiliesList.GetCurSel() == -1){
			m_DeleteFamily.EnableWindow(false);
	}else{
		m_DeleteFamily.EnableWindow(true);
		m_FamilyHouseList.ResetContent();
		int nId = (int)m_FamiliesList.GetItemData(m_FamiliesList.GetCurSel());
		for(std::map<int, housing>::iterator it = m_aHousings.begin(); it != m_aHousings.end(); it++){
			if(it->second.m_nFamily == nId)
				m_FamilyHouseList.SetItemData(m_FamilyHouseList.AddString(it->second.m_strName.c_str()), it->first);
		}
		m_FamilyHouseList.SetCurSel(0);
	}
}

void CHousingManagerDialog::OnHouseAdd(){
	CHousingDialog dialog;
	dialog.DoModal();
	int nId = dialog.GetIdHousing();
	std::tstring strName = dialog.GetNameHousing();
	m_aHousings.insert(std::make_pair(nId, housing(dialog.GetTypeHousing(), dialog.GetTypeFrames(), 0, strName)));
	m_HousingList.SetItemData(m_HousingList.AddString(strName.c_str()), nId);
	m_FreeHousing.SetItemData(m_FreeHousing.AddString(strName.c_str()), nId);
}

afx_msg void CHousingManagerDialog::OnHouseFamilyAdd(){
	if(m_FreeHousing.GetCurSel() == -1)
		return;
	int nSel = m_FreeHousing.GetCurSel();
	int nId = (int)m_FreeHousing.GetItemData(nSel);
	m_FreeHousing.DeleteString(nSel);
	m_FamilyHouseList.SetItemData(m_FamilyHouseList.AddString(m_aHousings[nId].m_strName.c_str()), nId);
	m_aHousings[nId].m_nFamily = (int)m_FamiliesList.GetItemData(m_FamiliesList.GetCurSel());
	m_FreeHousing.SetCurSel(0);
	UpdateHouseDb(nId);
}

void CHousingManagerDialog::OnHouseFamilyDelete(){
	if(m_FamilyHouseList.GetCount() == 1){
		MessageBox(ILanguageManager::GetLanguageManager()->GetString(HOUSE_UNCONNECT_ERROR_ID).c_str(),
					ILanguageManager::GetLanguageManager()->GetString(HOUSE_DIALOG_CAPTION_ID).c_str(), MB_OK);
		return;
	}
	int nSel = m_FamilyHouseList.GetCurSel();
	int nId = (int)m_FamilyHouseList.GetItemData(nSel);
	m_FamilyHouseList.DeleteString(nSel);
	m_FreeHousing.SetItemData(m_FreeHousing.AddString(m_aHousings[nId].m_strName.c_str()), nId);
	m_aHousings[nId].m_nFamily = 0;
	m_FamilyHouseList.SetCurSel(0);
	UpdateHouseDb(nId);
}

void CHousingManagerDialog::UpdateHouseDb(int nId){
	IDatabase* pDb = IDatabaseManager::GetDatabaseManager()->OpenDatabase(JOURNAL_DATABASE_NAME);
	CHousingsTable Housing;
	Housing.m_content.s_nId = nId;
	Housing.m_content.s_bDelete = 0;
	Housing.m_content.s_strName = m_aHousings[nId].m_strName;
	Housing.m_content.s_nType = m_aHousings[nId].m_nTypeHousing;
	Housing.m_content.s_nFrames = m_aHousings[nId].m_nTypeFrame;
	Housing.m_content.s_nIdFamily = m_aHousings[nId].m_nFamily;
	Housing.UpdateValue(pDb);
	IDatabaseManager::GetDatabaseManager()->CloseDatabase(pDb);
}

void CHousingManagerDialog::OnHouseChange(){
	int nSel = m_HousingList.GetCurSel();
	int nId = (int)m_HousingList.GetItemData(nSel);
	CHousingDialog dialog(nId, m_aHousings[nId].m_strName, m_aHousings[nId].m_nTypeHousing,
		m_aHousings[nId].m_nTypeFrame, m_aHousings[nId].m_nFamily);
	dialog.DoModal();

	m_HousingList.DeleteString(nSel);
	m_HousingList.SetItemData(m_HousingList.InsertString(nSel, dialog.GetNameHousing().c_str()), nId);
	m_HousingList.SetCurSel(nSel);
	m_NameHouse.SetWindowText(dialog.GetNameHousing().c_str());

	int temp = m_FamilyHouseList.FindString(0, m_aHousings[nId].m_strName.c_str());
	if(temp != -1){
		m_FamilyHouseList.DeleteString(temp);
		m_FamilyHouseList.SetItemData(m_FamilyHouseList.InsertString(temp, dialog.GetNameHousing().c_str()), nId);
		m_FamilyHouseList.SetCurSel(temp);
	}

	temp = m_FreeHousing.FindString(0, m_aHousings[nId].m_strName.c_str());
	if(temp != -1){
		m_FreeHousing.DeleteString(temp);
		m_FreeHousing.SetItemData(m_FreeHousing.InsertString(temp, dialog.GetNameHousing().c_str()), nId);
		m_FreeHousing.SetCurSel(temp);
	}

	m_aHousings[nId].m_strName = dialog.GetNameHousing();
	m_aHousings[nId].m_nTypeHousing = dialog.GetTypeHousing();
	m_aHousings[nId].m_nTypeFrame = dialog.GetTypeFrames();
}

void CHousingManagerDialog::OnHouseDelete(){
	int nSel = m_HousingList.GetCurSel();
	int nId = (int)m_HousingList.GetItemData(nSel);
	if(m_aHousings[nId].m_nFamily){
		MessageBox(ILanguageManager::GetLanguageManager()->GetString(HOUSE_DELETE_ERROR_ID).c_str(),
					ILanguageManager::GetLanguageManager()->GetString(HOUSE_DIALOG_CAPTION_ID).c_str(), MB_OK);
		return;
	}

	std::tstring strContent = ILanguageManager::GetLanguageManager()->GetString(QUESTION_DELETE_ID) +
		_T(" '") + m_aHousings[nId].m_strName + _T("'.");
	if(MessageBox(strContent.c_str(), ILanguageManager::GetLanguageManager()->
		GetString(DELETE_HOUSING_ID).c_str(), MB_YESNO) == IDNO)
		return;

	m_HousingList.DeleteString(nSel);
	m_HousingList.SetCurSel(0);
	OnHouseSelChange();

	int temp = m_FreeHousing.FindString(0, m_aHousings[nId].m_strName.c_str());
	if(temp != -1){
		m_FreeHousing.DeleteString(temp);
		m_FreeHousing.SetCurSel(0);
	}

	m_aHousings.erase(nId);

	IDatabase* pDb = IDatabaseManager::GetDatabaseManager()->OpenDatabase(JOURNAL_DATABASE_NAME);
	CHousingsTable Housing;
	Housing.m_content.s_nId = nId;
	Housing.DeleteValue(pDb);
	IDatabaseManager::GetDatabaseManager()->CloseDatabase(pDb);
}